package com.wanxi.dao.impl;

import com.wanxi.dao.ProductDao;
import com.wanxi.model.ProductModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product(" +
                "`name`,`normal_price`,`price`," +
                "`content`,is_show,is_recommend," +
                "img_href,create_time,update_time," +
                "enable,where_show,cuisine_id) values('" + productModel.getName() + "','" + productModel.getNormalPrice() + "','" + productModel.getMarketPrice() + "'," +
                "'" + productModel.getContent() + "','1','1'," +
                "'" + productModel.getImgHref() + "',now(),now()," +
                "'1','3','" + productModel.getCuisineId() + "')";
        return JDBC.update(sql);
    }

    @Override
    public int enable(ProductModel productModel) {
//        如果数据库里面以前是0，则这届修改为1，如果是1，则直接修改为0
//        String sql = "update product set enable=" + productModel.getEnable() + " " +
//                "where id=" + productModel.getId();
//        String sql="UPDATE  product  SET enable = CASE enable WHEN 0 THEN 1 WHEN 1 THEN 0 END WHERE id="+productModel.getId();
//        String sql = "UPDATE  product  SET enable = 1-enable WHERE id = " + productModel.getId();
//        还有一种：异或
        String sql = "UPDATE  product  SET enable=enable^1 where id=" + productModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "update product set ";
        if (!"".equals(productModel.getName())) {
            sql += " name ='" + productModel.getName() + "',";

        }
        if (!"".equals(productModel.getImgHref())) {
            sql += " img_href='" + productModel.getImgHref() + "', ";
        }
        sql += " update_time = now() ";
        sql += " where id=" + productModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public int delete(ProductModel productModel) {
        String sql = "delete from product where id=" + productModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public ProductModel findById(ProductModel productModel) {
        String sql = "select * from product where id=" + productModel.getId();
        ResultSet resultSet = JDBC.query(sql);
        ProductModel model = null;
        try {
            while (resultSet.next()) {
                model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setMarketPrice(resultSet.getBigDecimal("price"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = " select * from (" +
                " select p.*,c.name cuisineName from product p " +
                " left join cuisine c " +
                " on p.cuisine_id=c.id) temp " +
                " where 1=1 ";
        sql += setWhereSql(productModel);
        sql += " order by id desc ";
        sql += " limit " + (productModel.getPage() - 1) * productModel.getLimit() + "," + productModel.getLimit();
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setMarketPrice(resultSet.getBigDecimal("price"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCuisineName(resultSet.getString("cuisineName"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setEnable(resultSet.getInt("enable"));
//                model.setMarketPriceString(resultSet.getString("price"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    private String setWhereSql(ProductModel productModel) {
        String sql = "";
        if (productModel.getIsShow() != null && productModel.getIsShow() != -1) {
            sql += " and temp.is_show =1 ";
        }
        if (productModel.getEnable() != -1) {
            sql += " and temp.enable =1 ";
        }
        if (!"".equals(productModel.getName())) {
            sql += " and temp.name like '%" + productModel.getName() + "%'";
        }
        if (productModel.getCuisineId() != -1) {
            sql += " and temp.cuisine_id=" + productModel.getCuisineId();
        }

        return sql;
    }

    @Override
    public int count(ProductModel productModel) {
        String sql = "select count(*) count from ( " +
                "select p.*,c.name cuisineName from product p " +
                "left join cuisine c " +
                "on p.cuisine_id=c.id) temp " +
                " where 1=1 ";
        sql += setWhereSql(productModel);
        return JDBC.count(sql);
//        ResultSet resultSet = JDBC.query(sql);
//        try {
//            while (resultSet.next()) {
//                return resultSet.getInt("count");
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return 0;
    }


}
